﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Reflection;

namespace BA2009.Utility
{
    public static class Reflection
    {
        /// <summary>
        /// Get string value from boxing object
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="objectToGet"></param>
        /// <returns></returns>
        public static string GetStringValue(string propertyName,object objectToGet)
        {
            //return GetObjectValue(propertyName, objectToGet).ToString();
            object value = GetObjectValue(propertyName, objectToGet);
            if (value != null)
                return value.ToString();
            else
                return string.Empty;
            
        }

        public static object GetObjectValue(string propertyNane, object objectToGet)
        {
            Type objectType = objectToGet.GetType();
            System.Reflection.PropertyInfo pi = objectType.GetProperty(propertyNane);
            if (pi == null)
                throw new InvalidOperationException(string.Format("Không có Property tên là {0} trong object {1}", propertyNane, objectToGet.ToString()));

            return pi.GetValue(objectToGet, null);
        }

        public static void SetObjectValue(string propertyNane, object objectToSet,object value)
        {
            Type objectType = objectToSet.GetType();
            System.Reflection.PropertyInfo pi = objectType.GetProperty(propertyNane);
            if (pi == null)
                throw new InvalidOperationException(string.Format("Không có Property tên là {0} trong object {1}", propertyNane, objectToSet.ToString()));

            pi.SetValue(objectToSet, value,null);
        }

        public static void InvokeMethod(object objectToInvoke, string methodName, params object[] inputParams)
        {
            Type objectType = objectToInvoke.GetType();
            System.Reflection.MethodInfo mi = objectType.GetMethod(methodName);
            mi.Invoke(objectToInvoke, inputParams);
        }


        /// <summary>
        /// Is source type contain of equal to destination type
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        /// <returns></returns>
        public static bool IsContainOrEqualType(Type source, Type destination)
        { 
            //if(source.BaseType==destination)
            Type type = source;
            while (type.BaseType != null)
            {
                if (type == destination)
                    return true;
                type = type.BaseType;
            }

            return false;
            
        }


        public static bool IsContainOrEqual(this Type source, Type destination)
        {
            //if(source.BaseType==destination)
            Type type = source;
            while (type.BaseType != null)
            {
                if (type == destination)
                    return true;
                type = type.BaseType;
            }

            return false;
        }

       
    }
}
